package com.ran.sys.entity.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.ran.common.vo.MD5;
import com.ran.common.vo.Result;
import com.ran.sys.entity.Admin;
import com.ran.sys.entity.User;
import com.ran.sys.entity.UserStatus;
import com.ran.sys.entity.Userinfo;
import com.ran.sys.mapper.ManagerMapper;
import com.ran.sys.mapper.StatusMapper;
import com.ran.sys.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StatusMapper statusMapper;

    @GetMapping("/info")
    public Result<Map<String, Object>> info(@RequestBody User user){
        Userinfo userinfo=managerMapper.selectInfo(user.getUsername());
        Map<String,Object> map =new HashMap<>();
        map.put("aa",userinfo);
        return Result.success(map);
    }

    @PostMapping("/login")
    public Result<Map<String,Object>> login(@RequestBody Admin admin){
        if (!admin.getUsername().equals("admin")){
            return Result.fail("该管理员不存在");
        }
        if (!admin.getPassword().equals("123456")){
            return Result.fail("密码错误");
        }
        return Result.success("登录成功");
    }



    @GetMapping("/getStatus")
    public Result<Map<String,Object>> getStatus(){
        List<UserStatus> list=statusMapper.selectList(null);


        Map<String,Object> map =new HashMap<>();
        map.put("打卡记录",list);
        return Result.success(map);
    }

    @PutMapping("/update")
     public Result<Map<String,Object>> update(@RequestBody User user){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId,user.getId());
        User user1=userMapper.selectOne(wrapper);
        user.setFace(user1.getFace());
        if (user.getPassword()==null){
            user.setPassword(user1.getPassword());
        }
        user.setPassword(MD5.encrypt(user.getPassword()));
        int update = userMapper.update(user, wrapper);
        if (update==0){
            return Result.fail("查无此人，修改失败");
        }
        return Result.success("修改成功");
    }

    @GetMapping("/rate")
    public Result<Map<String,Object>> rate(){
        double peopleSum=statusMapper.peopleSum();
        double successSum=statusMapper.successSum();
        double failSum=statusMapper.failSum();
        double recordSum=statusMapper.recordSum();
        Map<String,Object> map =new HashMap<>();
        map.put("打卡率",successSum/peopleSum);
        map.put("人脸通过率",recordSum==0?0:1-failSum/recordSum);
        map.put("异常打卡率",recordSum==0?0:failSum/recordSum);
        return Result.success(map);
    }

    @GetMapping("/list")
    public Result<Map<String,Object>> list(){
        List<User> list=userMapper.selectList(null);
        Map<String,Object> map =new HashMap<>();
        map.put("所有人员",list);
        return Result.success(map);
    }






}
